package com.kxmall.admin.controller.vehicle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kxmall.admin.controller.user.service.IKxUserService;
import com.kxmall.admin.controller.vehicle.service.IKxVehicleOrderService;
import com.kxmall.common.core.domain.PageQuery;
import com.kxmall.common.core.page.TableDataInfo;
import com.kxmall.common.utils.BeanCopyUtils;
import com.kxmall.user.domain.KxUser;
import com.kxmall.user.mapper.KxUserMapper;
import com.kxmall.vehicle.domain.KxVehicleBoxGoodsOrder;
import com.kxmall.vehicle.domain.KxVehicleBoxUpGoods;
import com.kxmall.vehicle.domain.KxVehicleInGoods;
import com.kxmall.vehicle.domain.KxVehicleOrder;
import com.kxmall.vehicle.domain.bo.KxVehicleOrderBo;
import com.kxmall.vehicle.domain.vo.KxUserGoodsOrderVo;
import com.kxmall.vehicle.domain.vo.KxVehicleOrderVo;
import com.kxmall.vehicle.mapper.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 订单Service业务层处理
 */
@RequiredArgsConstructor
@Service
@Slf4j
public class KxVehicleOrderServiceImpl implements IKxVehicleOrderService {
    @Autowired
    private KxUserMapper userMapper;
    @Autowired
    private KxVehicleOrderMapper vehicleOrderMapper;

    @Autowired
    private KxVehicleBoxUpGoodsMapper vehicleBoxUpGoodsMapper;

    @Autowired
    private KxVehicleBoxGoodsOrderMapper vehicleBoxGoodsOrderMapper;

    @Autowired
    private KxVehicleInGoodsMapper vehicleInGoodsMapper;

    @Autowired
    private final IKxUserService kxUserService;

    /**
     * 查询订单
     */
    @Override
    public KxVehicleOrderVo queryById(Long id) {
        KxVehicleOrderVo vehicleOrderVo = new KxVehicleOrderVo();
        KxVehicleBoxGoodsOrder kxVehicleBoxGoodsOrder = vehicleBoxGoodsOrderMapper.selectById(id);
        if(kxVehicleBoxGoodsOrder != null){
            BeanCopyUtils.copy(kxVehicleBoxGoodsOrder, vehicleOrderVo);
            KxUser user = userMapper.selectById(kxVehicleBoxGoodsOrder.getUserId());
            if(user != null){
                vehicleOrderVo.setUserName(user.getUsername());
            }
            KxVehicleInGoods vehicleInGoods = vehicleInGoodsMapper.selectById(kxVehicleBoxGoodsOrder.getInGoodsId());
            if(vehicleInGoods != null) {
                vehicleOrderVo.setProductId(vehicleInGoods.getProductId());
                vehicleOrderVo.setProductName(vehicleInGoods.getProductName());
                vehicleOrderVo.setProductAttrId(vehicleInGoods.getProductAttrId());
                vehicleOrderVo.setProductAttrName(vehicleInGoods.getProductAttrName());
                vehicleOrderVo.setCategoryId(vehicleInGoods.getCategoryId());
                vehicleOrderVo.setCategoryName(vehicleInGoods.getCategoryName());
            }
        }
        return vehicleOrderVo;
    }

    /**
     * 查询订单列表
     */
    @Override
    public TableDataInfo<KxVehicleOrderVo> queryPageList(KxVehicleOrderBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<KxVehicleBoxGoodsOrder> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, KxVehicleBoxGoodsOrder::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getVehicleNo()), KxVehicleBoxGoodsOrder::getVehicleNo, bo.getVehicleNo());
        lqw.like(StringUtils.isNotBlank(bo.getVehicleName()), KxVehicleBoxGoodsOrder::getVehicleName, bo.getVehicleName());
        lqw.eq(bo.getBoxIndex() != null, KxVehicleBoxGoodsOrder::getBoxIndex, bo.getBoxIndex());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderNo()), KxVehicleBoxGoodsOrder::getOrderNo, bo.getOrderNo());
        lqw.eq(bo.getOrderStatus() != null, KxVehicleBoxGoodsOrder::getOrderStatus, bo.getOrderStatus());
        if (ObjectUtils.isNotEmpty(bo.getStartTime()) && ObjectUtils.isNotEmpty(bo.getEndTime())) {
            lqw.between(KxVehicleBoxGoodsOrder::getCreateTime, bo.getStartTime(), bo.getEndTime());
        }
        Page<KxVehicleBoxGoodsOrder> vehicleBoxGoodsOrderPage = vehicleBoxGoodsOrderMapper.selectPage(pageQuery.build(), lqw);
        List<KxVehicleOrderVo> vehicleOrderVos = new ArrayList<>();
        for(KxVehicleBoxGoodsOrder kxVehicleBoxGoodsOrder : vehicleBoxGoodsOrderPage.getRecords()){
            KxVehicleOrderVo vehicleOrderVo = new KxVehicleOrderVo();
            BeanCopyUtils.copy(kxVehicleBoxGoodsOrder, vehicleOrderVo);
            KxUser user = userMapper.selectById(kxVehicleBoxGoodsOrder.getUserId());
            if(user != null){
                vehicleOrderVo.setUserName(user.getUsername());
            }
            KxVehicleInGoods vehicleInGoods = vehicleInGoodsMapper.selectById(kxVehicleBoxGoodsOrder.getInGoodsId());
            if(vehicleInGoods != null) {
                vehicleOrderVo.setProductId(vehicleInGoods.getProductId());
                vehicleOrderVo.setProductName(vehicleInGoods.getProductName());
                vehicleOrderVo.setProductAttrId(vehicleInGoods.getProductAttrId());
                vehicleOrderVo.setProductAttrName(vehicleInGoods.getProductAttrName());
                vehicleOrderVo.setCategoryId(vehicleInGoods.getCategoryId());
                vehicleOrderVo.setCategoryName(vehicleInGoods.getCategoryName());
            }
            vehicleOrderVos.add(vehicleOrderVo);
        }
        Page<KxVehicleOrderVo> result = new Page<>();
        result.setRecords(vehicleOrderVos);
        result.setTotal(vehicleBoxGoodsOrderPage.getTotal());
        result.setSize(vehicleBoxGoodsOrderPage.getSize());
        result.setCurrent(vehicleBoxGoodsOrderPage.getCurrent());
        result.setPages(vehicleBoxGoodsOrderPage.getPages());
        return TableDataInfo.build(result);
    }

    /**
     * 查询订单列表
     */
    @Override
    public List<KxVehicleOrderVo> queryList(KxVehicleOrderBo bo) {
        LambdaQueryWrapper<KxVehicleBoxGoodsOrder> lqw = Wrappers.lambdaQuery();
        lqw.eq(((bo.getId() != null) && (bo.getId() > 0)), KxVehicleBoxGoodsOrder::getId, bo.getId());
        lqw.eq(((bo.getUserId() != null) && (bo.getUserId() > 0)), KxVehicleBoxGoodsOrder::getUserId, bo.getUserId());
        //lqw.like(StringUtils.isNotBlank(bo.getUserName()), KxVehicleBoxGoodsOrder::getUserName, bo.getUserName());
        lqw.eq(StringUtils.isNotBlank(bo.getVehicleNo()), KxVehicleBoxGoodsOrder::getVehicleNo, bo.getVehicleNo());
        lqw.like(StringUtils.isNotBlank(bo.getVehicleName()), KxVehicleBoxGoodsOrder::getVehicleName, bo.getVehicleName());
        lqw.eq(((bo.getBoxIndex() != null) && (bo.getBoxIndex() > 0)), KxVehicleBoxGoodsOrder::getBoxIndex, bo.getBoxIndex());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderNo()), KxVehicleBoxGoodsOrder::getOrderNo, bo.getOrderNo());
        lqw.eq(((bo.getOrderStatus() != null) && (bo.getOrderStatus() > -2)), KxVehicleBoxGoodsOrder::getOrderStatus, bo.getOrderStatus());
        if ((bo.getStartTime() != null && bo.getStartTime() > 0)
                && (bo.getEndTime() != null && bo.getEndTime() > 0)) {
            lqw.between(KxVehicleBoxGoodsOrder::getCreateTime, new Date(bo.getStartTime()), new Date(bo.getEndTime()));
        }
        List<KxVehicleBoxGoodsOrder> vehicleBoxGoodsOrderList = vehicleBoxGoodsOrderMapper.selectList(lqw);
        List<KxVehicleOrderVo> vehicleOrderVoList = new ArrayList<>();
        for(KxVehicleBoxGoodsOrder kxVehicleBoxGoodsOrder : vehicleBoxGoodsOrderList){
            KxVehicleOrderVo vehicleOrderVo = new KxVehicleOrderVo();
            BeanCopyUtils.copy(kxVehicleBoxGoodsOrder, vehicleOrderVo);
            KxUser user = userMapper.selectById(kxVehicleBoxGoodsOrder.getUserId());
            if(user != null){
                vehicleOrderVo.setUserName(user.getUsername());
            }
            KxVehicleInGoods vehicleInGoods = vehicleInGoodsMapper.selectById(kxVehicleBoxGoodsOrder.getInGoodsId());
            if(vehicleInGoods != null) {
                vehicleOrderVo.setProductId(vehicleInGoods.getProductId());
                vehicleOrderVo.setProductName(vehicleInGoods.getProductName());
                vehicleOrderVo.setProductAttrId(vehicleInGoods.getProductAttrId());
                vehicleOrderVo.setProductAttrName(vehicleInGoods.getProductAttrName());
                vehicleOrderVo.setCategoryId(vehicleInGoods.getCategoryId());
                vehicleOrderVo.setCategoryName(vehicleInGoods.getCategoryName());
            }
            vehicleOrderVoList.add(vehicleOrderVo);
        }
        return vehicleOrderVoList;
    }

    /**
     * 修改订单
     */
    //@Override
    //public Boolean updateByBo(KxVehicleOrderBo bo) {
    //    return true;
    //}

    /**
     * 批量删除订单
     */
    //@Override
    //public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
    //    return true;
    //}

}
